//
//  WKCustomerServiceManager.m
//  WKCustomerService
//
//  Created by LQ on 2022/4/1.
//

#import "WKCustomerServiceManager.h"
#import <WuKongBase/WuKongBase.h>
#import <WuKongBase/UIDevice+Utils.h>




@implementation WKCustomerServiceManager

static WKCustomerServiceManager *_instance;


+ (id)allocWithZone:(NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [super allocWithZone:zone];
    });
    return _instance;
}
+ (WKCustomerServiceManager *)shared
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] init];
        
    });
    return _instance;
}

-(AnyPromise*) visitorLoginOrRegister {
    NSString *vid = [WKApp shared].loginInfo.uid;
    NSString *appName = [WKApp shared].config.appName;
    
    UIDevice *device = [UIDevice currentDevice];
    NSString *deviceName = [device model];
    NSString *systemName = [device systemName];
    NSString *deviceModel =  [UIDevice getDeviceModel];
    NSString *systemVersion = [device systemVersion];
    
    NSTimeZone *zone = [NSTimeZone localTimeZone];
    
    
    return [[WKAPIClient sharedClient] POST:[NSString stringWithFormat:@"hotline/widget/%@/visitor",self.appID] parameters:@{
        @"vid":vid,
        @"site_title":appName,
        @"not_register_token": @(1),
        @"device": @{
            @"device":deviceName,
            @"os":systemName,
            @"model":deviceModel,
            @"version": systemVersion,
        },
        @"timezone": zone.name,
        @"local":@(1)
    }];
}

-(AnyPromise*) visitorTopicChannelGetOrCreate {
   return [[WKAPIClient sharedClient] POST:@"hotline/visitor/topic/channel" parameters:@{
    @"topic_id":@(0),
   } headers:@{
        @"appid": self.appID,
    }];
}

-(NSString*) visitorMsgChannelID:(NSString*)channelID channelType:(NSInteger)channelType{
    if(channelType == WK_CustomerService) {
        return [NSString stringWithFormat:@"%@|%@",[WKApp shared].loginInfo.uid,channelID];
    }
    return channelID;
}

@end
